package com.itheima.utils;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Map;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2022/12/13 10:41
 */
@Slf4j
public class ExcelExportEngine {

    public static Workbook writeExcel(Object obj, Workbook workbook, String imgPath) throws Exception {
        //把查到的数据转成map中的key
        Map<String, Object> map = EntityUtils.entityToMap(obj);

        //取出工作表
        Sheet sheet = workbook.getSheetAt(0);

        //遍历行数据
        Row row = null;
        Cell cell = null;
        for (int i = 0; i < 100; i++) {
            row = sheet.getRow(i);
            //判断如果 行等于空就跳过
            if (row != null) {
                //取列
                for (int j = 0; j < 100; j++) {
                    cell = row.getCell(j);
                    //判断非空
                    if (cell != null) {
                        writeToCell(cell, map);
                    }
                }
            }
        }

        if (imgPath != null) {

            //照片位置
            //先创建一个字节输出流
            ByteOutputStream outputStream = new ByteOutputStream();
            // 读取图片 放入一个带有缓存区的图片类中 ImageIO类             绝对路径
            BufferedImage image = ImageIO.read(new File(imgPath));
            //取后缀名   .toUpperCase();转大写
            String extName = imgPath.substring(imgPath.lastIndexOf(".") + 1).toUpperCase();
            //然后把图片写入到字节输出流中
            ImageIO.write(image, extName, outputStream); //写

            //Patriarch 负责创建图片  ClientAnchor负责设置图片大小和位置
            Drawing patriarch = sheet.createDrawingPatriarch(); //Patriarch

            //动态获取开始列行和结束列行
            Sheet sheet1 = workbook.getSheetAt(1);
            int col1 = ((Double) sheet1.getRow(0).getCell(0).getNumericCellValue()).intValue();
            int row1 = ((Double) sheet1.getRow(0).getCell(1).getNumericCellValue()).intValue();
            int col2 = ((Double) sheet1.getRow(0).getCell(2).getNumericCellValue()).intValue();
            int row2 = ((Double) sheet1.getRow(0).getCell(3).getNumericCellValue()).intValue();

            //指定图片位置   前四个是设置偏移量   后四个是设置位置   开始列3    开始行2    结束列4    结束行5
            ClientAnchor clientAnchor = new XSSFClientAnchor(0, 0, 0, 0, col1, row1, col2, row2); //ClientAnchor

            //开始把图片写入到sheet指定的位置
            switch (extName) {
                case "JPG": {
                    patriarch.createPicture(clientAnchor, workbook.addPicture(outputStream.toByteArray(),
                            XSSFWorkbook.PICTURE_TYPE_JPEG));
                    break;
                }
                case "PNG": {
                    patriarch.createPicture(clientAnchor, workbook.addPicture(outputStream.toByteArray(),
                            XSSFWorkbook.PICTURE_TYPE_PNG));
                    break;
                }
                default: {
                    break;
                }
            }

            //上传完成后删除不要的工作表
            workbook.removeSheetAt(1);
        }

        return workbook;
    }


    private static void writeToCell(Cell cell, Map<String, Object> map) {

        CellType cellType = cell.getCellType();
        switch (cellType) {
            case FORMULA: {  //判断如果是公式的话什么都不干
                break;
            }
            default: {
                String value = cell.getStringCellValue();
                if (StringUtils.isNotBlank(value)) {
                    for (String key : map.keySet()) {
                        if (key.equals(value)) {
                            cell.setCellValue(map.get(key).toString());
                        }
                    }
                }
            }
        }
    }
}
